// 1 导入ps和path
const fs = require('fs')

const path = require('path')

// 2 定义正则

const regStyle = /<style>([\s\S]*)<\/style>/

const regScript = /<script>([\s\S]*)<\/script>/

fs.readFile(path.join(__dirname, '/index.html'), 'utf-8', function (err, data) {
    if (err) return console.log('读取文件失败：' + err.message)
    resolveCss(data)
    resolveJs(data)
    resolveHtml(data)
})


const resolveCss = (data) => {
    const css = regStyle.exec(data)[1]

    fs.writeFile(path.join(__dirname, 'clock/index.css'), css, 'utf-8', (err) => {
        if (err) return console.log('css创建失败' + err.message)
        console.log('css创建成功');
    })
}

const resolveJs = (data) => {
    const js = regScript.exec(data)[1]
    fs.writeFile(path.join(__dirname, 'clock/index.js'), js, 'utf-8', (err) => {
        if (err) return console.log('js创建失败' + err.message)
        console.log('js创建成功');
    })
}

const resolveHtml = (data) => {
    const htmlStr = data.replace(regStyle, '<link rel="stylesheet" href="./index.css">').replace(regScript, '<script src="./index.js"></script>')

    fs.writeFile(path.join(__dirname, './clock/index.html'), htmlStr, 'utf-8', (err) => {
        if (err) return console.log('html创建成功' + err.message);
        console.log('html创建成功');
    })
}